<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleExport()" v-auth="'btn:export:zucaiduizhao'">
          <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
        <a-button type="default" @click="handleImport()" v-auth="'btn:import:zucaiduizhao'">
          <Icon icon="ant-design:upload-outlined" /> {{ t('导入') }}
        </a-button>
        <a-button type="primary" @click="handleForm({})" v-auth="'btn:add:zucaiduizhao'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
        <a-popconfirm
          title="是否确认批量删除？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handleBatchDel()"
        >
          <a-button type="danger" v-auth="'btn:beatchdelete:zucaiduizhao'">
            <Icon icon="ant-design:delete-outlined" /> {{ t('批量删除') }}
          </a-button>
        </a-popconfirm>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ mcode: record.mcode })">
          {{ record.mcode }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsMaterialMainPmMaterialMainList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    pmMaterialMainDelete,
    pmMaterialMainListData,
    pmMaterialMainBatchDel,
  } from '/@/api/material/main/pmMaterialMain';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';

  const { t } = useI18n('material.main.pmMaterialMain');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('主材对照表管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('材料编码'),
        field: 'mcode',
        component: 'Input',
      },
      {
        label: t('材料名称'),
        field: 'mname',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('材料编码'),
      dataIndex: 'mcode',
      key: 'a.mcode',
      sorter: true,
      width: 100,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('材料名称'),
      dataIndex: 'mname',
      key: 'a.mname',
      sorter: true,
      width: 240,
      align: 'left',
    },
    {
      title: t('规格型号'),
      dataIndex: 'mstd',
      key: 'a.mstd',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('单位'),
      dataIndex: 'munit',
      key: 'a.munit',
      sorter: true,
      width: 60,
      align: 'center',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑主材对照表'),
        onClick: handleForm.bind(this, { mcode: record.mcode }),
        auth: 'btn:edit:zucaiduizhao',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除主材对照表'),
        popConfirm: {
          title: t('是否确认删除主材对照表'),
          confirm: handleDelete.bind(this, { mcode: record.mcode }),
        },
        auth: 'btn:delete:zucaiduizhao',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRowKeys }] = useTable({
    api: pmMaterialMainListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/material/main/pmMaterialMain/exportData',
      target: '_self',
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  async function handleDelete(record: Recordable) {
    const res = await pmMaterialMainDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handleBatchDel() {
    if (getSelectRowKeys().length) {
      const res = await pmMaterialMainBatchDel({ selIds: getSelectRowKeys().join(',') });
      showMessage(res.message);
      handleSuccess();
    } else {
      showMessage('请先选择一条数据！');
    }
  }

  function handleSuccess() {
    reload();
  }
</script>
